package c_leetcode.a_1_tow_sum;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 哈希表
 * 时间复杂度    O(n)
 * 空间复杂度    O(n)
 *
 * @author FRSF
 * @since 2025/2/23 13:48
 */
public class HashTable {

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        HashTable hashTable = new HashTable();
        System.out.println(Arrays.toString(hashTable.twoSum(nums, target)));
    }

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int search = target - nums[i];
            if (map.containsKey(search)) {
                return new int[]{i, map.get(search)};
            }
            map.put(nums[i], i);
        }
        return new int[0];
    }
}
